<?php
Class Review extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
		$this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->data['controller'] = 'review';
        $this->load->model('Mreview');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 11, $this->session->userdata('admin_id'));
        
    }
    
    public function index()
    {
	//die('3');
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mreview->getReviewDB($config['per_page'],$offset);
		
		//echo '<pre>'.print_r($data);die(';');
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data;
        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        if($this->form_validation->run() === TRUE)
        {
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Mreview->deleteReviewDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/review/index";</script>');
            }
        }
        $this->data['action'] = 'index';
        $this->load->view('template/layout', $this->data);
    }
	
    public function editreview()
    {
	
		if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
	
        $this->load->library('form_validation');
		$this->form_validation->set_rules('restaurant_id', 'Nhà hàng', 'required');  
			//$this->form_validation->set_rules('member_id', 'Người dùng', 'required');
		$this->form_validation->set_rules('star', 'Số sao', 'required');
        $this->form_validation->set_rules('content', 'Nội dung', 'required');
		$id = $this->getParamUri(4,1);
		$data = $this->Mreview->getReviewInfo($id);
		//die(print_r($data));
		$this->data['getReviewInfo'] = $data;
		$this->data['optionRestaurant'] = $this->Mreview->optionRestaurant($data->rid);
        $this->data['optionMember'] = $this->Mreview->optionMember($data->mid);
		
		$this->data['nameCustomer'] = $this->Mreview->nameCustomer($data->name_re,$data->mid);
		
		$this->data['optionStar'] = $this->Mreview->optionStar($data->star);
        if($this->form_validation->run() === TRUE)
        {
            $restaurant_id = $this->getParamInt('restaurant_id');
			//$member_id = $this->getParamInt('member_id');
			$star = $this->getParamInt('star');
            $content = $this->getParamString('content');
			$improved = $this->getParamString('improved'); 
			if( $improved == TRUE || $improved == 'on' ){ $improved = 1;}else{ $improved = 0;}
            $this->data['data'] = $this->Mreview->editReview($id,$restaurant_id,$star,$content,$improved);
			if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa thành công");window.location.href="'.$this->data['baseurl'].'manage/review/edit-review/'.$id.'";</script>');
            }
        }
		$this->data['action'] = 'editreview';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deletereview()
    {
	
		if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
	
        $id = $this->getParamUri(4,1);
        $run = $this->Mreview->deleteReviewDb($id);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/review/index";</script>');
        }
    }
    
}